package com.isman.placeslist;

import com.google.gson.Gson;
import com.isman.placeslist.apiobject.Status.User;
import com.isman.placeslist.util.Util;
import com.isman.placeslist.ws.AsyncRunnerHeadler;
import com.isman.placeslist.ws.ServiceProvider;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.ScrollView;
import android.widget.TextView;

public class TabUserDetail extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tab_user_detail);

		mFriendUserId = getIntent().getExtras().getLong(PageUser.USER_ID);
		init();
		vscView.setVisibility(View.GONE);
		vProgressBar.setVisibility(View.VISIBLE);
		initService();
	}

	private void init() {
		vlblName = (TextView) findViewById(R.id.lbl_uDetail_name);
		vlblPlace = (TextView) findViewById(R.id.lbl_uDetail_place);
		vlblFriendCount = (TextView) findViewById(R.id.lbl_uDetail_friendCount);
		vlblFriendMutual = (TextView) findViewById(R.id.lbl_uDetail_multuer);
		vlblLikeCount = (TextView) findViewById(R.id.lbl_uDetail_likeCount);
		vlblPictureCount = (TextView) findViewById(R.id.lbl_uDetail_pictureCount);
		vlblCheckInCount = (TextView) findViewById(R.id.lbl_uDetail_checkinCount);

		// Add Friend
		vbtnAddRequest = (Button) findViewById(R.id.btn_uDetail_addrequest);
		vbtnCancelRequest = (Button) findViewById(R.id.btn_uDetail_cancelRequest);
		vbtnAcceptFriend = (Button) findViewById(R.id.btn_uDetail_acceptfriend);
		vbtnUnaccepFriend = (Button) findViewById(R.id.btn_uDetail_unacceptfriend);
		vbtnBlockFriend = (Button) findViewById(R.id.btn_uDetail_blockfriend);

		// For friend
		vbtnCapture = (Button) findViewById(R.id.btn_uDetail_Capture);
		vbtnStatus = (Button) findViewById(R.id.btn_uDetail_status);

		// People
		vimgPeople[0] = (ImageView) findViewById(R.id.img_inc_prople1);
		vimgPeople[1] = (ImageView) findViewById(R.id.img_inc_prople2);
		vimgPeople[2] = (ImageView) findViewById(R.id.img_inc_prople3);
		vimgPeople[3] = (ImageView) findViewById(R.id.img_inc_prople4);
		vimgProfile = (ImageView) findViewById(R.id.img_uDetail_profile);
		vllContactBody = (LinearLayout) findViewById(R.id.ll_uDetail_contactBody);
		vibtnMore = (ImageButton) findViewById(R.id.ibtn_inc_more);

		vscView = (ScrollView) findViewById(R.id.ScrollView1);
		vProgressBar = (ProgressBar) findViewById(R.id.progressBar1);

		vibtnMore.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {

			}
		});
		vbtnAddRequest.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mAddRequest();
			}
		});
		vbtnCancelRequest.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mCancelRequest();
			}
		});
		vbtnAcceptFriend.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mAccepRequest();
			}
		});
		vbtnUnaccepFriend.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mUnaccepRequest();
			}
		});
		vbtnBlockFriend.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mBlockFriend();
			}
		});
		vbtnCapture.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mCapture();
			}
		});
		vbtnStatus.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mStatus();
			}
		});
	}

	private void initService() {
		ServiceProvider provider = new ServiceProvider(this);
		provider.getFriendDetail(mFriendUserId, new AsyncRunnerHeadler() {
			@Override
			public void onComplete(final String json) {
				mFriendUser = new Gson().fromJson(json, User.class);
				
				runOnUiThread(new Runnable() {
					@Override
					public void run() {
						initData();

						vscView.setVisibility(View.VISIBLE);
						vProgressBar.setVisibility(View.GONE);
					}
				});
				Util.getBitmap(mFriendUser.getPicture(), TabUserDetail.this, vimgProfile);
				mLoadPeople();
			}

			@Override
			public void onAuthenticate(String json) {
			}

			@Override
			public void onError(int statusCode, String json) {
			}

			@Override
			public void onConnectingError(String message) {
			}
		});

	}

	private void initData() {
		vlblName.setText(mFriendUser.getName());
		vlblFriendCount.setText(mFriendUser.getInfo().getFriendCount() + "");
		vlblFriendMutual.setText("(" + mFriendUser.getInfo().getFriendMutail()
				+ ")");
		vlblCheckInCount.setText(mFriendUser.getInfo().getCheckInCount() + "");
		vlblLikeCount.setText(mFriendUser.getInfo().getLikeCount() + "");
		vlblPictureCount.setText(mFriendUser.getInfo().getPictureCount() + "");
		vlblPlace.setText(mFriendUser.getInfo().getLastPlace());

		if (mFriendUser.getInfo().getFriendCount() == 0) {
			vibtnMore.setVisibility(View.VISIBLE);
		}
		Log.e("TAG", mFriendUser.getInfo().getFriendStatus() + "");
		switch (mFriendUser.getInfo().getFriendStatus()) {
		case 0:
			vbtnAddRequest.setVisibility(View.VISIBLE);
			vbtnCancelRequest.setVisibility(View.GONE);
			vbtnAcceptFriend.setVisibility(View.GONE);
			vbtnUnaccepFriend.setVisibility(View.GONE);
			vbtnBlockFriend.setVisibility(View.GONE);
			break;
		case 1:
			vbtnAddRequest.setVisibility(View.GONE);
			vbtnCancelRequest.setVisibility(View.GONE);
			vbtnAcceptFriend.setVisibility(View.VISIBLE);
			vbtnUnaccepFriend.setVisibility(View.VISIBLE);
			vbtnBlockFriend.setVisibility(View.VISIBLE);
			break;
		case 2:
			vbtnAddRequest.setVisibility(View.GONE);
			vbtnCancelRequest.setVisibility(View.VISIBLE);
			vbtnAcceptFriend.setVisibility(View.GONE);
			vbtnUnaccepFriend.setVisibility(View.GONE);
			vbtnBlockFriend.setVisibility(View.GONE);
			break;
		case 3:
			vbtnAddRequest.setVisibility(View.GONE);
			vbtnCancelRequest.setVisibility(View.GONE);
			vbtnAcceptFriend.setVisibility(View.GONE);
			vbtnUnaccepFriend.setVisibility(View.GONE);
			vbtnBlockFriend.setVisibility(View.GONE);
			break;
		}
	}

	private void mLoadPeople() {
		if (mFriendUser.getInfo().getFriendOfFriend().length == 0) {
			vimgPeople[0].setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {

				}
			});
		}

		for (int i = 0; i < mFriendUser.getInfo().getFriendOfFriend().length; i++) {
			final ImageView img = vimgPeople[i];
			final long userIds = mFriendUser.getInfo().getFriendOfFriend()[i]
					.getId();
			runOnUiThread(new Runnable() {
				@Override
				public void run() {
					img.setVisibility(View.VISIBLE);
					img.setScaleType(ScaleType.FIT_XY);
					img.setOnClickListener(new OnClickListener() {
						@Override
						public void onClick(View v) {
							Intent intent = new Intent(TabUserDetail.this,
									PageUser.class);
							intent.putExtra(PageUser.USER_ID, userIds);
							startActivity(intent);
						}
					});
				}
			});
			Util.getBitmap(
					mFriendUser.getInfo().getFriendOfFriend()[i].getPicture(),
					TabUserDetail.this, img);
		}
	}

	private void mAddRequest() {
		ServiceProvider provider = new ServiceProvider(this);
		provider.addRequestFriend(mFriendUserId, new AsyncRunnerHeadler() {
			@Override
			public void onComplete(String json) {
				runOnUiThread(new Runnable() {
					@Override
					public void run() {
						mFriendUser.getInfo().setFriendStatus(2);
						initData();
					}
				});
			}

			@Override
			public void onAuthenticate(String json) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onError(int statusCode, String json) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onConnectingError(String message) {
				// TODO Auto-generated method stub

			}
		});
	}

	private void mCancelRequest() {
		ServiceProvider provider = new ServiceProvider(this);
		provider.cancelRequestFriend(mFriendUserId, new AsyncRunnerHeadler() {
			@Override
			public void onComplete(String json) {
				runOnUiThread(new Runnable() {
					@Override
					public void run() {
						mFriendUser.getInfo().setFriendStatus(0);
						initData();
					}
				});
			}

			@Override
			public void onAuthenticate(String json) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onError(int statusCode, String json) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onConnectingError(String message) {
				// TODO Auto-generated method stub

			}
		});
	}

	private void mAccepRequest() {
		ServiceProvider provider = new ServiceProvider(this);
		provider.acceptFriend(mFriendUserId, new AsyncRunnerHeadler() {
			@Override
			public void onComplete(String json) {
				runOnUiThread(new Runnable() {
					@Override
					public void run() {
						mFriendUser.getInfo().setFriendStatus(3);
						initData();
					}
				});
			}

			@Override
			public void onAuthenticate(String json) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onError(int statusCode, String json) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onConnectingError(String message) {
				// TODO Auto-generated method stub

			}
		});
	}

	private void mUnaccepRequest() {
		ServiceProvider provider = new ServiceProvider(this);
		provider.unAcceptFriend(mFriendUserId, new AsyncRunnerHeadler() {
			@Override
			public void onComplete(String json) {
				runOnUiThread(new Runnable() {
					@Override
					public void run() {
						mFriendUser.getInfo().setFriendStatus(0);
						initData();
					}
				});
			}

			@Override
			public void onAuthenticate(String json) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onError(int statusCode, String json) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onConnectingError(String message) {
				// TODO Auto-generated method stub

			}
		});
	}

	private void mBlockFriend() {
		ServiceProvider provider = new ServiceProvider(this);
		provider.blockFriend(mFriendUserId, new AsyncRunnerHeadler() {
			@Override
			public void onComplete(String json) {
				runOnUiThread(new Runnable() {
					@Override
					public void run() {
						mFriendUser.getInfo().setFriendStatus(4);
						initData();
					}
				});
			}

			@Override
			public void onAuthenticate(String json) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onError(int statusCode, String json) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onConnectingError(String message) {
				// TODO Auto-generated method stub

			}
		});
	}

	private void mCapture() {
		Intent intent = new Intent(TabUserDetail.this, PageNewStatus.class);
		intent.putExtra(PageNewStatus.POST_TO, mFriendUserId);
		intent.putExtra(PageNewStatus.POST_TYPE, PageNewStatus.TOUSERFEED);
		intent.putExtra(PageNewStatus.POST_EVENT, PageNewStatus.CAPTURE);
		startActivityForResult(intent, 1);
	}

	private void mStatus() {
		Intent intent = new Intent(TabUserDetail.this, PageNewStatus.class);
		intent.putExtra(PageNewStatus.POST_TO, mFriendUserId);
		intent.putExtra(PageNewStatus.POST_TYPE, PageNewStatus.TOUSERFEED);
		intent.putExtra(PageNewStatus.POST_EVENT, PageNewStatus.NORMAL);
		startActivityForResult(intent, 0);
	}

	private long mFriendUserId;
	private User mFriendUser;
	private TextView vlblPlace;
	private TextView vlblName;
	private TextView vlblFriendCount;
	private TextView vlblFriendMutual;
	private TextView vlblCheckInCount;
	private TextView vlblLikeCount;
	private TextView vlblPictureCount;

	private Button vbtnAddRequest;
	private Button vbtnCancelRequest;
	private Button vbtnAcceptFriend;
	private Button vbtnUnaccepFriend;
	private Button vbtnBlockFriend;

	private Button vbtnStatus;
	private Button vbtnCapture;
	private ImageView[] vimgPeople = new ImageView[4];
	private ImageView vimgProfile;
	private ImageButton vibtnMore;
	private LinearLayout vllContactBody;

	private ScrollView vscView;
	private ProgressBar vProgressBar;
}
